package com.frxuexi.lib_base.utils

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.frxuexi.lib_base.R

object GlideUtils {

    fun getImage(iv:ImageView,url:String){
        Glide.with(iv).load(url)
            .placeholder(R.drawable.home_icon)
            .error(R.drawable.home_icon)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .into(iv)
    }

    fun getCircleImage(iv:ImageView,url:String){
        Glide.with(iv).load(url)
            .placeholder(R.drawable.home_icon)
            .error(R.drawable.home_icon)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .circleCrop()
            .into(iv)
    }

    fun getRoundImage(iv:ImageView,url:String){
        Glide.with(iv).load(url)
            .placeholder(R.drawable.home_icon)
            .error(R.drawable.home_icon)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(20)))
            .into(iv)
    }
}

@BindingAdapter("url")
fun loadImage(iv:ImageView,url:String?){
    Glide.with(iv).load(url)
        .placeholder(R.drawable.home_icon)
        .error(R.drawable.home_icon)
        .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
        .apply(RequestOptions.bitmapTransform(RoundedCorners(20)))
        .into(iv)
}

